package com.example.front;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import java.util.Map;

public class ThymeleafEngine {
    private static final TemplateEngine ENGINE = new TemplateEngine();
    private static final ClassLoaderTemplateResolver RESOLVER = new ClassLoaderTemplateResolver();

    static {
        // 模板文件放在 /templates/ 目录下（可按需修改）
        RESOLVER.setPrefix("templates/");
        RESOLVER.setSuffix(".html");
        RESOLVER.setTemplateMode("HTML");
        RESOLVER.setCharacterEncoding("UTF-8");
        RESOLVER.setCacheable(false); // 开发时可关缓存
        ENGINE.setTemplateResolver(RESOLVER);
    }

    public static String process(String templateName, Map<String, Object> model) {
        Context ctx = new Context();
        ctx.setVariables(model);
        return ENGINE.process(templateName, ctx);
    }
}